package com.guigu.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guigu.bean.User;
import com.guigu.bean.UserExample;
import com.guigu.bean.UserExample.Criteria;
import com.guigu.dao.UserMapper;

@Service
public class UserServices {

	@Autowired
	UserMapper userMapper;
	
	/**
	 * 用户保存方法
	 * @param user
	 */
	public void saveUser(User user){
		userMapper.insertSelective(user);
	}
	
	/**
	 * 返回所有用户信息
	 * @return
	 */
	public List<User> getAllUser(){
		return userMapper.selectByExample(null);
	}
	
	/**
	 * 检测邮箱是否已经存在
	 * true表示可用，false表示不可用
	 * @param email
	 * @return
	 */
	public boolean checkUser(String email){
		UserExample userExample=new UserExample();
		Criteria criteria=userExample.createCriteria();
		criteria.andEmailEqualTo(email);
		long count=userMapper.countByExample(userExample);
		return count==0;
	}
	
	/**
	 * 通过邮箱检索用户，找到则返回第一个(默认邮箱不重复)
	 * @param email
	 * @return
	 */
	public User getUserByEmail(String email){
		UserExample userExample=new UserExample();
		Criteria criteria=userExample.createCriteria();
		criteria.andEmailEqualTo(email);
		return userMapper.selectByExample(userExample).get(0);
	}

	/**
	 *修改用户
	 * @param user
	 */
	public void updateUser(User user) {
		userMapper.updateByPrimaryKeySelective(user);
	}

	/**
	 * 删除用户
	 * @param userid
	 */
	public void deleteUserByUserId(Integer userid) {
		userMapper.deleteByPrimaryKey(userid);
	}
	

}
